/**
 * Created by gongxun on 17/3/17.
 */

var KwxApplyTableLayer = GameCCSBase.extend({
    mGameName : null,

    //ui
    Panel_center : null,
    mLeftTime : null,
    Text_time : null,

    //逻辑
    mShengqingLogicSeat : null,
    ctor: function (gameName) {
        this._super();
        this.mGameName = gameName;
        this.loadCocostudio("res/ccs/KwxApplyTableLayer.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        this.Text_time = mo.seekWidgetByName(this.ccsNode, "Text_time");

        mo.seekWidgetByName(this.ccsNode, "Button_cancel").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendCancelTableRefuse();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendCancelTableAgree();
            }
        }, this);

        this.setVisible(false);
    },

    onEnter : function(){
        this._super();
        // this.showAction(this.Panel_center);

        this.mLeftTime = 0;
        this.schedule(function(dt){
            this.mLeftTime -= dt;
            if (this.mLeftTime < 0){
                this.mLeftTime = 0;
            }
            mo.gameHelper.setTextString(this.Text_time, parseInt(this.mLeftTime) + "秒后将解散房间");
        }, this);
    },

    onExit : function(){
        this._super();
    },

    setShowThis : function(){
        this.stopAllActions();
        this.setVisible(true);
        this.mLeftTime = 180;
    },

    setCloseThis : function(){
        this.mCancelJu = null;
        this.mShengqingLogicSeat = null;

        this.stopAllActions();
        this.runAction(cc.sequence(
            cc.delayTime(1),
            cc.hide()
        ));
    },

    updateMySelectEnabled : function(){
        if (this.mCancelJu == null){
            return;
        }

        var mySelect = false;
        mySelect = this.mCancelJu["uid" + this.getTableStatus().MySeat] == 0;

        mo.seekWidgetByName(this.ccsNode, "Button_cancel").setVisible(mySelect);
        mo.seekWidgetByName(this.ccsNode, "Button_ok").setVisible(mySelect);
        mo.seekWidgetByName(this.ccsNode, "Image_wait").setVisible(!mySelect);
    },

    updateCancelJuInfo : function(cancleJu) {
        if (this.mCancelJu == null){
            this.mCancelJu = cancleJu;
            this.mShengqingLogicSeat = null;

            if (cancleJu.uid1 == 111){
                this.mShengqingLogicSeat = 1;
            }else if (cancleJu.uid2 == 111){
                this.mShengqingLogicSeat = 2;
            }else if (cancleJu.uid3 == 111){
                this.mShengqingLogicSeat = 3;
            }else if (cancleJu.uid4 == 111 ){
                this.mShengqingLogicSeat = 4;
            }else if (cancleJu.uid5 == 111 ){
                this.mShengqingLogicSeat = 5;
            }

            if (this.mShengqingLogicSeat != null){
                this.setShowThis();
            }else{
                this.setCloseThis();
                return;
            }
        }else{
            this.mCancelJu = cancleJu;
        }

        //更新按钮
        this.updateMySelectEnabled();

        //到这里说明有人选择了
        //没有排序 只有简单的申请人放前面
        var userInfos = [];
        var userInfoLogics = [];

        if (this.getTableStatus()["SeatUser" + this.mShengqingLogicSeat] != null){
            userInfos.push(this.getTableStatus()["SeatUser" + this.mShengqingLogicSeat].user_info);
            userInfoLogics.push(this.mShengqingLogicSeat);
        }
        for (var i = 1; i <= 5; i++ ){
            if (i == this.mShengqingLogicSeat){
                continue;
            }
            if (this.getTableStatus()["SeatUser" + i] != null){
                userInfos.push(this.getTableStatus()["SeatUser" + i].user_info);
                userInfoLogics.push(i);
            }
        }

        var userCount = userInfos.length;
        for (var i = 0; i < 5; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Panel_player" + i);
            if (i >= userCount){
                player.setVisible(false);
            }else{
                player.setVisible(true);
                var offset = 110;
                player.setPositionX(-((userCount - 1) * offset) / 2 + i * offset);

                var Image_shenqing = mo.seekWidgetByName(player, "Image_shenqing");
                var Text_wait = mo.seekWidgetByName(player, "Text_wait");
                var Text_jujue = mo.seekWidgetByName(player, "Text_jujue");
                var Text_tongyi = mo.seekWidgetByName(player, "Text_tongyi");

                Image_shenqing.setVisible(false);
                Text_wait.setVisible(false);
                Text_jujue.setVisible(false);
                Text_tongyi.setVisible(false);

                //nick
                mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_nick"), mo.gameHelper.getUserNickFilter(userInfos[i].nickname));
                //head
                mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), userInfos[i].faceUrl);

                if (userInfoLogics[i] == this.mShengqingLogicSeat){
                    Image_shenqing.setVisible(true);
                }else{
                    var code = this.mCancelJu["uid" + userInfoLogics[i]];
                    if (code == 0){
                        Text_wait.setVisible(true);
                    }else if (code == 111){
                        Text_tongyi.setVisible(true);
                    }else if (code == 222){
                        Text_jujue.setVisible(true);
                        this.setCloseThis();
                        return;
                    }
                }
            }
        }
    },

    //同意
    sendCancelTableAgree : function(){
        var code = 111;
        var myLogicSeat = this.getTableStatus().MySeat;
        if (this.mGameName == "kwx"){
            mo.gameMsgManager.sendCancleJuRequestkwx(myLogicSeat, code);
        }else if (this.mGameName == "nn"){
            mo.gameMsgManager.sendCancleJuRequestnn(myLogicSeat, code);
        }else if (this.mGameName == "gsbdx"){
            mo.gameMsgManager.sendCancleJuRequestgsbdx(myLogicSeat, code);
        }else if (this.mGameName == "psz"){
            mo.gameMsgManager.sendCancleJuRequestpsz(myLogicSeat, code);
        }else if (this.mGameName == "whmj"){
            mo.gameMsgManager.sendCancleJuRequestwhmj(myLogicSeat, code);
        }else if (this.mGameName == "dzpk"){
            mo.gameMsgManager.sendCancleJuRequestdzpk(myLogicSeat, code);
        }else if (this.mGameName == "dtj"){
            mo.gameMsgManager.sendCancleJuRequestdtj(myLogicSeat, code);
        }else if (this.mGameName == "pdk"){
            mo.gameMsgManager.sendCancleJuRequestpdk(myLogicSeat, code);
        }
    },

    //拒绝
    sendCancelTableRefuse : function(){
        var code = 222;
        var myLogicSeat = this.getTableStatus().MySeat;
        if (this.mGameName == "kwx"){
            mo.gameMsgManager.sendCancleJuRequestkwx(myLogicSeat, code);
        }else if (this.mGameName == "nn"){
            mo.gameMsgManager.sendCancleJuRequestnn(myLogicSeat, code);
        }else if (this.mGameName == "gsbdx"){
            mo.gameMsgManager.sendCancleJuRequestgsbdx(myLogicSeat, code);
        }else if (this.mGameName == "psz"){
            mo.gameMsgManager.sendCancleJuRequestpsz(myLogicSeat, code);
        }else if (this.mGameName == "whmj"){
            mo.gameMsgManager.sendCancleJuRequestwhmj(myLogicSeat, code);
        }else if (this.mGameName == "dzpk"){
            mo.gameMsgManager.sendCancleJuRequestdzpk(myLogicSeat, code);
        }else if (this.mGameName == "dtj"){
            mo.gameMsgManager.sendCancleJuRequestdtj(myLogicSeat, code);
        }else if (this.mGameName == "pdk"){
            mo.gameMsgManager.sendCancleJuRequestpdk(myLogicSeat, code);
        }
    },

    //返回tableStars
    getTableStatus : function(){
        if (this.mGameName == "kwx"){
            return mo.kwxTableStatus.mTableStatus;
        }else if (this.mGameName == "nn"){
            return mo.nnTableStatus.mTableStatus;
        }else if (this.mGameName == "gsbdx"){
            return mo.gsbdxTableStatus.mTableStatus;
        }else if (this.mGameName == "psz"){
            return mo.pszTableStatus.mTableStatus;
        }else if (this.mGameName == "whmj"){
            return mo.whmjTableStatus.mTableStatus;
        }else if (this.mGameName == "dzpk"){
            return mo.dzpkTableStatus.mTableStatus;
        }else if (this.mGameName == "dtj"){
            return mo.dtjTableStatus.mTableStatus;
        }else if (this.mGameName == "pdk"){
            return mo.pdkTableStatus.mTableStatus;
        }
    }
});
